package com.lga.algorithm;


import java.util.Scanner;

/**
 *7-1 最大子列和问题
 * @author  lga
 */
public class MaximumSubsequenceSum {


    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        //输入第1行给出正整数K(≤100000)。
        int k = Integer.parseInt(input.nextLine());
        //第2行给出K个整数，其间以空格分隔。
        String numStr = input.nextLine();
        String[] numStrAarray = numStr.split(" ");
        int[] numIntAarray = new int[k];
        for (int i = 0; k > i; i++) {
                numIntAarray[i] =Integer.parseInt(numStrAarray[i]);
        }

        //输出最大的和
        System.out.println(getMaxSubsequenceSum(k, numIntAarray));

    }

    /**
     * 获取最大子列和的核心算法
     * @param k
     * @param numIntAarray
     * @return
     */
    private static int getMaxSubsequenceSum(int k, int[] numIntAarray) {
        int thisSum = 0;
        int maxSum = 0;
        for (int i = 0; i < k; i++) {
            thisSum += numIntAarray[i];
            if (thisSum > maxSum) {
                maxSum = thisSum;
            } else if (thisSum < 0) {
                thisSum = 0;
            }
        }
        return maxSum;
    }
}
